package com.yyk.yykywd.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewpager.widget.PagerAdapter
import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.config.HttpUrl
import com.yyk.yykywd.mvvm.adapter.ImageViewAdapter
import com.yyk.yykywd.mvvm.entity.AgentDetail1Bean
import com.yyk.yykywd.mvvm.entity.AgentDetailBean
import com.yyk.yykywd.util.DensityUtils
import com.yyk.yykywd.util.GlideUtils
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.widght.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_work_detail.*
import kotlinx.android.synthetic.main.activity_work_detail1.*
import kotlinx.android.synthetic.main.fragment_agent_site.view.*
import kotlinx.android.synthetic.main.fragment_proxy_site.view.*

/**
 * 文件描述：
 * Created by guo on 2019-09-05.
 */
class MyPagerAdapter(private val mContext: Context,
                     private val type: String,
                     private val mData: List<String>,
                     private val detail: AgentDetail1Bean)
    : PagerAdapter() {
    override fun getCount(): Int {
        return mData.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view: View
        if (position == 0) {
            view = View.inflate(mContext, R.layout.fragment_agent_site, null)
            detail.data?.run {
                if (ptype == 1) {
                    view.tv_agent_type.text = "个人"
                    view.tv_id_photo_lint.text = "代理商身份证照片"
                    view.tv_business_licence_lint.visibility = View.GONE
                    view.rv_business_licence.visibility = View.GONE
                } else {
                    view.tv_agent_type.text = "公司"
                    view.tv_id_photo_lint.text = "法人身份证照片"
                    view.tv_business_licence_lint.visibility = View.VISIBLE
                    view.rv_business_licence.visibility = View.VISIBLE
                }
                view.tv_agent_name.text = agentname
                view.tv_contact.text = phone
                view.tv_agent_region.text = area
                view.tv_agent_source.text = source
                if (trade.isNullOrBlank()) {
                    view.tv_agent_industry_lint.visibility = View.GONE
                    view.tv_agent_industry.visibility = View.GONE
                } else {
                    view.tv_agent_industry_lint.visibility = View.VISIBLE
                    view.tv_agent_industry.visibility = View.VISIBLE
                    view.tv_agent_industry.text = trade
                }
                if (desc.isNullOrBlank()) {
                    view.tv_agent_describe_lint.visibility = View.GONE
                    view.tv_agent_describe.visibility = View.GONE
                } else {
                    view.tv_agent_describe_lint.visibility = View.VISIBLE
                    view.tv_agent_describe.visibility = View.VISIBLE
                    view.tv_agent_describe.text = desc
                }

                //代理商企业营业执照
                var licences: MutableList<String> = ArrayList()
                business_img?.run {
                    val list = stringToList(this)
                    for (s in list) {
                        licences.add(s)
                    }
                    val licenceAdapter = ImageViewAdapter(mContext, R.layout.item_iv, licences)
                    view.rv_business_licence.adapter = licenceAdapter
                    licenceAdapter.setOnItemClickListener { _, _, position ->
                        ToAnotherActivity.toPicActivity(mContext, HttpUrl.BASE_URL + licences[position])
                    }
                    view.rv_business_licence.layoutManager = GridLayoutManager(mContext, 5)
                    view.rv_business_licence.addItemDecoration(SpaceItemDecoration(DensityUtils.dp2px(mContext, 10f)))
                }

                var idFront: String = front
                var idBack: String = dorsal
                GlideUtils.loadRoundCircleImage(mContext, HttpUrl.BASE_URL + idFront, view.iv_id_front, 4)
                GlideUtils.loadRoundCircleImage(mContext, HttpUrl.BASE_URL + idBack, view.iv_id_back, 4)
                view.iv_id_front.setOnClickListener {
                    ToAnotherActivity.toPicActivity(mContext, HttpUrl.BASE_URL + idFront)
                }
                view.iv_id_back.setOnClickListener {
                    ToAnotherActivity.toPicActivity(mContext, HttpUrl.BASE_URL + idBack)
                }
            }
        } else {
            view = View.inflate(mContext, R.layout.fragment_proxy_site, null)
            detail.info[position - 1]?.run {
                if (info1.flag == 2) {
                    view.tv_contract_info.text = "合同信息-定金合同"
                    if (transition == 1) {
                        view.cl_all_contract.visibility = View.VISIBLE
                        view.tv_all_contact_num.text = info2.order_code_1
                    } else {
                        view.cl_all_contract.visibility = View.GONE
                    }
                } else {
                    view.tv_contract_info.text = "合同信息-全款合同"
                    view.cl_all_contract.visibility = View.GONE
                }

                if (is_show == 2) {
                    view.cl_refund.visibility = View.VISIBLE
                    //退款申请表
                    val refundApply: ArrayList<String> = ArrayList()
                    //退款确认单
                    val refundConfirm: ArrayList<String> = ArrayList()
                    info3?.let {
                        refundApply.addAll(stringToList(it.apply_pic))
                        refundConfirm.addAll(stringToList(it.affirm_pic))
                        val applyAdapter = ImageViewAdapter(mContext, R.layout.item_iv, refundApply)
                        view.rv_refund_apply_form.layoutManager = GridLayoutManager(mContext, 5)
                        view.rv_refund_apply_form.addItemDecoration(SpaceItemDecoration(DensityUtils.dp2px(mContext, 10f)))
                        view.rv_refund_apply_form.adapter = applyAdapter
                        applyAdapter.setOnItemClickListener { _, _, position ->
                            ToAnotherActivity.toPicActivity(mContext, HttpUrl.BASE_URL + refundApply[position])
                        }
                        val confirmAdapter = ImageViewAdapter(mContext, R.layout.item_iv, refundConfirm)
                        view.rv_refund_confirmation.layoutManager = GridLayoutManager(mContext, 5)
                        view.rv_refund_confirmation.addItemDecoration(SpaceItemDecoration(DensityUtils.dp2px(mContext, 10f)))
                        view.rv_refund_confirmation.adapter = confirmAdapter
                        confirmAdapter.setOnItemClickListener { _, _, position ->
                            ToAnotherActivity.toPicActivity(mContext, HttpUrl.BASE_URL + refundConfirm[position])
                        }
                        view.tv_refund_reason.text = it.cause
                        if (it.remark.isNullOrBlank()) {
                            view.tv_refund_remark_hint.visibility = View.GONE
                            view.tv_refund_remark.visibility = View.GONE
                        } else {
                            view.tv_refund_remark_hint.visibility = View.VISIBLE
                            view.tv_refund_remark.visibility = View.VISIBLE
                            view.tv_refund_remark.text = it.remark
                        }
                    }

                } else {
                    view.cl_refund.visibility = View.GONE
                }
                view.tv_status.text = tag_clients
                if (agency_name.isNullOrBlank()) {
                    view.tv_agent_site_name_lint.visibility = View.GONE
                    view.tv_agent_site_name.visibility = View.GONE
                } else {
                    view.tv_agent_site_name_lint.visibility = View.VISIBLE
                    view.tv_agent_site_name.visibility = View.VISIBLE
                    view.tv_agent_site_name.text = agency_name
                }
                view.tv_agent_site_type.text = status
                view.tv_agent_price.text = agency_money.toString() + "元"
                when (type) {
                    FROM_RETURN_MONEY_AGENT -> {
                        view.tv_still_need.visibility = View.GONE
                        view.tv_still_need_lint.visibility = View.GONE
                        if (is_show == 2) {
                            view.cl_refund_info.visibility = View.VISIBLE
                            view.tv_already.text = already_money.toString() + "元"
                            view.tv_refund.text = tk_money.toString() + "元"
                            view.tv_status.setBackgroundResource(R.drawable.shape_2dp2)
                            view.tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.col_e61a13))
                        } else {
                            view.cl_refund_info.visibility = View.GONE
                            view.tv_status.setBackgroundResource(R.drawable.shape_2dp3)
                            view.tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.gray_aa))
                        }
                    }
                    FROM_SIGN_CONTRACT_AGENT -> {
                        view.tv_still_need.visibility = View.VISIBLE
                        view.tv_still_need_lint.visibility = View.VISIBLE
                        view.tv_still_need.text = rxhk_money.toString() + "元"
                        view.tv_status.setBackgroundResource(R.drawable.shape_2dp2)
                        view.tv_status.setTextColor(ContextCompat.getColor(mContext, R.color.col_e61a13))
                    }
                }
                view.tv_contact_type.text = getContractType(info1.flag)
                view.tv_contact_num.text = info1.order_code
                //合同图片
                var contracts: ArrayList<String> = ArrayList()
                //全款合同图片
                var allContracts: ArrayList<String> = ArrayList()
                for (photo in info1.pic) {
                    val path = photo.path
                    when (photo.type) {
                        1 -> {
                            contracts.add(path)
                        }
                    }
                }
                for (pic in info2.pic) {
                    allContracts.add(pic.path)
                }

                view.tv_contract_img.setOnClickListener {
                    ToAnotherActivity.toContractImgActivity(mContext, contracts)
                }
                view.tv_all_contract_img.setOnClickListener {
                    ToAnotherActivity.toContractImgActivity(mContext, allContracts)
                }
            }
        }

        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

}